# Maintainer: J. Peter Mugaas <jpmugaas@suddenlink.net>

_realname=fonttools
pkgbase=mingw-w64-python-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-python-${_realname}")
provides=("${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
conflicts=("${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
replaces=("${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
pkgver=4.8.1
pkgrel=1
pkgdesc="Converts OpenType and TrueType fonts to and from XML (mingw-w64)"
arch=('any')
url='https://github.com/fonttools/fonttools'
license=('MIT')
depends=("${MINGW_PACKAGE_PREFIX}-python"
         "${MINGW_PACKAGE_PREFIX}-python-numpy")
optdepends=("${MINGW_PACKAGE_PREFIX}-python-brotli: WOFF 1.0 and WOFF 2.0 webfonts"
            "${MINGW_PACKAGE_PREFIX}-python-pyfilesystem2: for fontTools.ufoLib: to read/write UFO fonts"
            "${MINGW_PACKAGE_PREFIX}-python-pyzopfli: WOFF 1.0 and WOFF 2.0 webfonts"
            "${MINGW_PACKAGE_PREFIX}-python-sympy: for fontTools.misc.symfont, module for symbolic font statistics analysis")
install=${_realname}3-${CARCH}.install
makedepends=("${MINGW_PACKAGE_PREFIX}-python-matplotlib"
             "${MINGW_PACKAGE_PREFIX}-python-setuptools")
checkdepends=("${MINGW_PACKAGE_PREFIX}-python-pytest-runner")
options=('staticlibs' 'strip' '!debug')
source=("https://github.com/fonttools/fonttools/releases/download/${pkgver}/fonttools-${pkgver}.zip")
sha256sums=('596c0e8399c650dbf1048b7ca6fc7b12bd79a7db0981f3b87743de8c2b7512de')

prepare() {
  rm -rf python-build-${CARCH}
  cp -r "${_realname}-${pkgver}" "python-build-${CARCH}"
}

build() {
  msg "Python build for ${CARCH}"
  cd "${srcdir}/python-build-${CARCH}"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
    ${MINGW_PREFIX}/bin/python setup.py build
}

#Enable tests when the dependencies are solved
check() {
  msg "Python test for ${CARCH}"
  cd "${srcdir}/python-build-${CARCH}"
  ${MINGW_PREFIX}/bin/python setup.py pytest || warning "Tests failed"
}

package() {
  cd "${srcdir}/python-build-${CARCH}"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
  ${MINGW_PREFIX}/bin/python setup.py install --prefix=${MINGW_PREFIX} \
    --root="${pkgdir}" --optimize=1 --skip-build

  local PREFIX_WIN=$(cygpath -wm ${MINGW_PREFIX})
  # fix python command in files
  for _ff in ${pkgdir}${MINGW_PREFIX}/bin/*.py; do
    sed -e "s|${PREFIX_WIN}|${MINGW_PREFIX}|g" -i ${_ff}
  done

  install -D -m755 LICENSE "${pkgdir}${MINGW_PREFIX}"/share/licenses/python-${_realname}/LICENSE
  chmod oga+r "${pkgdir}${MINGW_PREFIX}"/share/man/man1/ttx.1
}
